//
//  MovieViewController.m
//  个人项目(豆瓣元旦作业)
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 guoguo. All rights reserved.
//

#import "MovieViewController.h"
#import "MovieHGTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#import "Movie.h";
@interface MovieViewController ()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic,retain) NSMutableArray *arr;
@property (nonatomic, retain) NSMutableArray *arrMovie;

@end

@implementation MovieViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"电影";
    
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:30/255.0 green:174/255.0 blue:230/255.0 alpha:1];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage  imageNamed:@"btn_nav_collection@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
    
    
    self.navigationItem.rightBarButtonItem = item;
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [self.view addSubview:_tableView];
    
    //注册
    
    [_tableView registerClass:[MovieHGTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    //数据处理
    NSString *pash = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"plist"];
    NSArray *arr = [NSMutableArray arrayWithContentsOfFile:pash];
    self.arrMovie = [NSMutableArray array];
    
    for (NSDictionary *dic in arr) {
        Movie *model = [Movie movie];
        
        [model setValuesForKeysWithDictionary:dic];
        
        [_arrMovie addObject:model];
    }

    
   

}




//item的点击方法

- (void)itemAction:(UIBarButtonItem *)item{
    
    ThirdViewController *TH = [[ThirdViewController alloc] init];
    
    [self.navigationController pushViewController:TH animated:YES];
    
    TH.arrTH = _arrMovie;

}






- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _arrMovie.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MovieHGTableViewCell *cell = [[MovieHGTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    
    cell.imageView1.image = [UIImage imageNamed:@"bg_eventlistcell@2x.png"];
    
    Movie *model = _arrMovie[indexPath.row];
    
    NSDictionary *dic = model.images;
    
    NSString *result = dic[@"medium"];
    
    
    [cell.imageView2 sd_setImageWithURL:[NSURL URLWithString:result]];
    
    
    cell.label.text = model.title;
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}

//点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SecondViewController *se = [[SecondViewController alloc] init];
    
    se.model = _arrMovie[indexPath.row];
    
    [self.navigationController pushViewController:se animated:YES];
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
